package com.ternnetwork.configuration.service.impl.job;



import javax.annotation.Resource;

import org.quartz.SchedulerException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.configuration.dao.job.QuartzJobDao;
import com.ternnetwork.configuration.dao.job.QuartzJobTriggerDao;
import com.ternnetwork.configuration.entity.job.QuartzJob;
import com.ternnetwork.configuration.entity.job.QuartzJobTrigger;
import com.ternnetwork.configuration.service.job.SystemJobService;
import com.ternnetwork.configuration.service.job.QuartzJobTriggerService;




@Service("quartzJobTriggerService")
public class QuartzJobTriggerServiceImpl implements QuartzJobTriggerService {
	
	@SuppressWarnings("restriction")
	@Resource
	private QuartzJobTriggerDao quartzJobTriggerDao;
	
	@SuppressWarnings("restriction")
	@Resource
	private QuartzJobDao quartzJobDao;
	
	@SuppressWarnings("restriction")
	@Resource
	private SystemJobService systemJobService;
	
	public QuartzJobTrigger doFindById(long id){
		return quartzJobTriggerDao.getReferenceById(QuartzJobTrigger.class, id);
	}
	@Override
	public BaseResponse idoAdd(QuartzJobTrigger t)throws Exception{
		
		BaseResponse res=new BaseResponse();
		
		if(StringUtils.isEmpty(t.getName())){
			res.setStatus(0);
			res.setMessage("操作失败，名称不能为空！");
			return res;
		}
		
		if(StringUtils.isEmpty(t.getGroup())){
			res.setStatus(1);
			res.setMessage("操作失败，分组不能为空！");
			return res;
		}
		
		if(StringUtils.isEmpty(t.getCronExpression())){
			res.setStatus(2);
			res.setMessage("操作失败，表达式不能为空！");
			return res;
		}
		
		Long totalCount=quartzJobTriggerDao.getTotalCount("select count(t.id) from QuartzJobTrigger t where t.name=?1", t.getName());
		
		if(totalCount.longValue()>0L){
			res.setStatus(3);
			res.setMessage("操作失败，名称已经存在！");
			return res;
		}
		quartzJobTriggerDao.persist(t);
		addJob(t.getJob().getId(),t);
		res.setStatus(4);
		res.setMessage("操作成功！");
		return res;
	}
	
	
	private void addJob(long jobId,QuartzJobTrigger quartzJobTriggerNew) throws SchedulerException, ClassNotFoundException{
		QuartzJob job=quartzJobDao.findById(QuartzJob.class,jobId);
		if(job.getEnable()==true){
			job.getTriggers().removeAll(job.getTriggers());
			job.getTriggers().add(quartzJobTriggerNew);
			systemJobService.startJob(job);
		}
	}
	
	private void updateJob(long jobId,QuartzJobTrigger oldQuartzJobTrigger,QuartzJobTrigger quartzJobTriggerNew) throws SchedulerException, ClassNotFoundException{
		QuartzJob job=quartzJobDao.findById(QuartzJob.class,jobId);
		if(job.getEnable()==true){
			job.getTriggers().add(oldQuartzJobTrigger);
			systemJobService.deleteJob(job);
			job.getTriggers().remove(oldQuartzJobTrigger);
			job.getTriggers().add(quartzJobTriggerNew);
			systemJobService.startJob(job);
		}
	}
	
	
	
	
	private void deleteJob(long jobId,QuartzJobTrigger quartzJobTriggerNew) throws SchedulerException, ClassNotFoundException{
		QuartzJob job=quartzJobDao.findById(QuartzJob.class,jobId);
		if(job.getEnable()==true){
			job.getTriggers().removeAll(job.getTriggers());
			job.getTriggers().add(quartzJobTriggerNew);
			systemJobService.deleteJob(job);
		}
	}
	@Override
	public BaseResponse idoUpdate(QuartzJobTrigger t)throws Exception{
		
		BaseResponse res=new BaseResponse();

		if(StringUtils.isEmpty(t.getName())){
			res.setStatus(0);
			res.setMessage("操作失败，名称不能为空！");
			return res;
		}
		if(StringUtils.isEmpty(t.getGroup())){
			res.setStatus(1);
			res.setMessage("操作失败，分组不能为空！");
			return res;
		}
		if(StringUtils.isEmpty(t.getCronExpression())){
			res.setStatus(2);
			res.setMessage("操作失败，表达式不能为空！");
			return res;
		}
		Long totalCount=quartzJobTriggerDao.getTotalCount("select count(t.id) from QuartzJobTrigger t where t.name=?1 and t.id!=?2", t.getName(),t.getId());

		if(totalCount.longValue()>0L){
			res.setStatus(3);
			res.setMessage("操作失败，名称已经存在！");
			return res;
		}
		QuartzJobTrigger oldQuartzJobTrigger=quartzJobTriggerDao.findById(QuartzJobTrigger.class, t.getId());
		quartzJobTriggerDao.saveOrUpdate(t);
		updateJob(t.getJob().getId(),oldQuartzJobTrigger,t);
		res.setStatus(4);
		res.setMessage("操作成功！");
		return res;
	}
	@Override
	public BaseResponse idoDelete(Long id)throws Exception{
		BaseResponse res=new BaseResponse();
		QuartzJobTrigger t=quartzJobTriggerDao.findById(QuartzJobTrigger.class, id);
		if(t==null){
			res.setStatus(0);
			res.setMessage("操作失败，对象不存在！");
			return res;
		}
		quartzJobTriggerDao.delete(t);
		systemJobService.deleteJobTrigger(t);
		res.setStatus(1);
		res.setMessage("操作成功！");
		return res;
	}

}
